package us.nchu.tomtiger.controller;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import us.nchu.tomtiger.enums.ResultEnum;
import us.nchu.tomtiger.model.*;
import us.nchu.tomtiger.service.*;
import us.nchu.tomtiger.utils.ResultUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by XiongXuan on 2018/1/11.
 */
@Controller
@RequestMapping("/adminASDF")
public class AdminController {
    @Resource
    private UserService userService;
    @Resource
    private ProjectService projectService;
    @Resource
    private UserPendingService userPendingService;
    @Resource
    private ProjectPendingService projectPendingService;
    @Resource
    private AdminService adminService;
    @Resource
    private UserShowService userShowService;

    @GetMapping
    public ModelAndView doAdminGet() {
        return new ModelAndView("/admin-login");
    }

    @PostMapping
    @ResponseBody
    public Result adminLogin(HttpServletRequest httpServletRequest, Admin admin) {
        Admin stdAdmin;
        try {
            stdAdmin = adminService.getAdminById(admin.getId());
        }catch (EmptyResultDataAccessException e) {
            return ResultUtil.getNoObjectInstance(ResultEnum.ERROR_LOGIN_NO_PHONENUMBER);
        }

        if (admin.getPassword().equals(stdAdmin.getPassword())) {
            // 登录成功
            httpServletRequest.getSession().setAttribute("adminId", admin.getId());
            return ResultUtil.success("登录成功");
        }
        else {
            return ResultUtil.getNoObjectInstance(ResultEnum.ERROR_LOGIN_WRONG_PASSWORD);
        }
    }

    @DeleteMapping
    @ResponseBody
    public Result adminLogout(HttpServletRequest httpServletRequest) {
        httpServletRequest.getSession().removeAttribute("adminId");
        return ResultUtil.success("退出成功");
    }

    @GetMapping("/center")
    public ModelAndView doAdminLoginGet() {
        return new ModelAndView("/admin-center");
    }

    @GetMapping("/admin-welcome")
    public ModelAndView doAdminWelcomeGet() {
        ModelAndView modelAndView = new ModelAndView("/admin-welcome");

        modelAndView.addObject("projectList", projectService.queryAllProject());
        modelAndView.addObject("projectPendingList", projectPendingService.getUncheckedList());
        modelAndView.addObject("userList", userService.queryAllUser());
        modelAndView.addObject("userPendingList", userPendingService.getUncheckedList());

        return modelAndView;
    }

    @GetMapping("/check-person")
    public ModelAndView doCheckPersonGet(HttpServletRequest httpServletRequest) {
        ModelAndView modelAndView = new ModelAndView("/check-person");

        List<UserPending> userPendingList = userPendingService.getUncheckedList();
        List<User> userList = userService.queryListByUserPendingList(userPendingList);
        modelAndView.addObject("userPendingList", userPendingList);
        modelAndView.addObject("userList", userList);
        modelAndView.addObject("adminId", httpServletRequest.getSession().getAttribute("adminId"));

        return modelAndView;
    }

    @PostMapping("/check-person")
    @ResponseBody
    public Result doCheckPerson(UserPending userPending, String adminId) {
        userPendingService.updateUserType(userPending, adminId);
        return ResultUtil.success("操作成功");
    }

    @GetMapping("/check-project")
    public ModelAndView doCheckPeopleGet(HttpServletRequest httpServletRequest) {
        ModelAndView modelAndView = new ModelAndView("/check-project");

        List<ProjectPending> projectPendingList = projectPendingService.getUncheckedList();
        List<Project> projectList = projectService.getProjectListByPendingList(projectPendingList);

        modelAndView.addObject("projectList", projectList);
        modelAndView.addObject("projectPendingList", projectPendingList);
        modelAndView.addObject("adminId", httpServletRequest.getSession().getAttribute("adminId"));

        return modelAndView;
    }

    @PostMapping("/check-project")
    @ResponseBody
    public Result doCheckProject(ProjectPending projectPending, String adminId) {
        projectPendingService.publishProject(projectPending, adminId);
        return ResultUtil.success("操作成功");
    }

    @GetMapping("/query-project")
    public ModelAndView doQueryProjectGet() {
        ModelAndView modelAndView = new ModelAndView("/query-project");

        List<Project> projectList = projectService.queryAllProject();
        modelAndView.addObject("projectList", projectList);

        return modelAndView;
    }

    @GetMapping("/query-person")
    public ModelAndView doQueryPersonGet() {
        ModelAndView modelAndView = new ModelAndView("/query-person");

        List<User> userList = userService.queryAllUser();
        modelAndView.addObject("userList", userList);
        modelAndView.addObject("userShowList", userShowService.queryForUserShowListByUserList(userList));

        return modelAndView;
    }

    @GetMapping("/query-project/{projectId}")
    public ModelAndView getModifyPersonPage(@PathVariable int projectId) {
        ModelAndView modelAndView = new ModelAndView("modify-project-info");

        Project project = projectService.getProjectById(projectId);
        modelAndView.addObject("projectId", projectId);
        modelAndView.addObject("name", project.getName());
        modelAndView.addObject("owner", project.getPhoneNumber());
        modelAndView.addObject("type", project.getType());
        modelAndView.addObject("contactType", project.getContactType());
        modelAndView.addObject("contact", project.getContact());
        modelAndView.addObject("field", project.getField());
        modelAndView.addObject("intro", project.getIntro());


        return modelAndView;
    }

    @PostMapping("/query-project")
    @ResponseBody
    public Result modifyProject(Project project) {
        System.out.println(project);
        projectService.updateProjectInfo(project);
        return ResultUtil.success("修改成功");
    }

    @DeleteMapping("/query-project")
    @ResponseBody
    public Result deletePerson(int projectId) {
        projectService.deleteProjectById(projectId);
        return ResultUtil.success("删除成功");
    }

    @GetMapping("/query-person/{phoneNumber}")
    public ModelAndView getModifyPersonPage(@PathVariable String phoneNumber) {
        ModelAndView modelAndView = new ModelAndView("modify-person-info");

        User user =  userService.getUserByPhoneNumber(phoneNumber);
        modelAndView.addObject("phoneNumber", user.getPhoneNumber());
        modelAndView.addObject("nickName", user.getNickName());
        modelAndView.addObject("userType", user.getUserType());
        modelAndView.addObject("contactType", user.getContact_type());
        modelAndView.addObject("contact", user.getContact());
        modelAndView.addObject("intro", user.getIntro());
        modelAndView.addObject("major", user.getMajor());
        modelAndView.addObject("schoolName", user.getSchoolName());

        return modelAndView;
    }

    @PostMapping("/query-person")
    @ResponseBody
    public Result modifyPerson(User user) {
        userService.updateUserInfoForAdmin(user);
        return ResultUtil.success("修改成功");
    }

    @DeleteMapping("/query-person")
    @ResponseBody
    public Result deletePerson(String phoneNumber) {
        userService.deleteUserByPhoneNumber(phoneNumber);
        return ResultUtil.success("删除成功");
    }

    @GetMapping("/admin-list")
    public ModelAndView getAdminListPage() {
        ModelAndView modelAndView = new ModelAndView("admin-list");

        List<Admin> adminList = adminService.getAdminList();

        modelAndView.addObject("adminList", adminList);

        return modelAndView;
    }
}
